package com.gray.blog.interceptor;

import com.google.common.collect.ImmutableSet;
import com.gray.blog.common.annotation.ArgCheckTrigger;
import com.gray.blog.common.enums.ErrorCodeEnum;
import com.gray.blog.common.util.JoinPointUtil;
import com.gray.blog.common.util.ResultUtil;
import com.gray.blog.interceptor.pointcut.ControllerPointCut;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * Created  By zhoux 2018/10/31
 **/
/*@Slf4j
@Aspect
@Order(2)
@Component
public class ArgInterceptor extends ControllerPointCut {

    private static final Set<String> ERROR_ARG = ImmutableSet.of("undefined");

    @Around("controllerMethod()")
    public Object argInterceptor(ProceedingJoinPoint joinPoint) throws Throwable {
        Method method = JoinPointUtil.ProceedingJoinPoint2Method(joinPoint);
        Object result = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        if (method == null) {
            log.info("IpInterceptor.argInterceptor method is null");
        } else {
            ArgCheckTrigger argCheckTrigger = method.getAnnotation(ArgCheckTrigger.class);
            if (argCheckTrigger != null) {
                Object[] arguments = joinPoint.getArgs();
                if (arguments != null) {
                    // 单个参数
                    Field[] fields = arguments[0].getClass().getDeclaredFields();
                    boolean checkResult = checkArgMethod(fields, arguments[0]);
                    if (!checkResult) {
                        ResultUtil.buildErrorResult(ErrorCodeEnum.PARAMS_ERROR, response);
                        return null;
                    }
                    if (arguments.length == 2) {
                        // 两个参数
                        Field[] fieldTwo = arguments[1].getClass().getDeclaredFields();
                        boolean checkTwoResult = checkArgMethod(fieldTwo, arguments[1]);
                        if (!checkTwoResult) {
                            ResultUtil.buildErrorResult(ErrorCodeEnum.PARAMS_ERROR, response);
                            return null;
                        }
                    }
                }
            }

        }
        result = joinPoint.proceed();
        return result;
    }

    private boolean checkArgMethod(Field[] fields, Object arg) {
        for (Field field : fields) {
            // 对于每个属性，获取属性名
            String varName = field.getName();
            try {
                // Java语言访问检查
                boolean access = field.isAccessible();
                if (!access) {
                    field.setAccessible(true);
                }
                Object object = field.get(arg);
                boolean result = ERROR_ARG.stream().anyMatch(data -> data.equals(object));
                if (result) {
                    log.error("ArgInterceptor error varName:{},value:{}", varName, object);
                    if (!access) {
                        field.setAccessible(false);
                    }
                    return false;
                }
                // 恢复Java语言访问检查
                if (!access) {
                    field.setAccessible(false);
                }
            } catch (Exception e) {
                log.error("ArgInterceptor error", e);
                return false;
            }
        }
        return true;
    }

}*/
